package huawei;

/**
 * 11 增强的str函数
 */
public class EnhancePatternStr {

    public int patternIndex (String orgStr, String p) {

        for (int i = 0; i < orgStr.length(); i++) {
            int ii = i;
            int jj = 0;

            boolean in = false;
            while (ii < orgStr.length() && jj < p.length()) {
                if (p.charAt(jj) == '[') {
                    in = true;
                    jj += 1;
                    continue;
                }
                if (p.charAt(jj) == ']') {
                    in = false;
                    jj += 1;
                    continue;
                }
                if (!in) {
                    // 不在括号中
                    if (p.charAt(jj) == orgStr.charAt(ii)) {
                        ii += 1;
                        jj += 1;
                    } else {
                        break;
                    }
                } else {
                    // 在括号中
                    boolean find = false;
                    while (jj < p.length() && p.charAt(jj) != ']') {
                        if (p.charAt(jj) == orgStr.charAt(ii)) {
                            find = true;
                        }
                        jj ++;
                    }
                    in = false;
                    if (!find) {
                        break;
                    } else {
                        ii += 1;
                        jj += 1;
                    }
                }

                if (jj == p.length()) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        EnhancePatternStr enhancePatternStr = new EnhancePatternStr();
        String s = "bcfdbcdd";
        String p = "ddd";
        System.out.println(enhancePatternStr.patternIndex(s, p));
    }
}
